package com.topisv.tms.web.phone;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.User;
import com.topisv.tms.entity.send.SendOut;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.exception.PaiUException;
import com.topisv.tms.service.LogisticsCompanyService;
import com.topisv.tms.service.account.AccountService;
import com.topisv.tms.service.send.SendOutService;
import com.topisv.tms.service.work.WorkOrderService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/phone")
public class WMSExpressController {
	
	private static final Logger log = LoggerFactory.getLogger(WMSExpressController.class);
	
	@Autowired
	private WorkOrderService workOrderService;
	@Autowired
	private SendOutService sendOutService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private LogisticsCompanyService logisticsCompanyService;
	/**
	 * 快递交接
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "ajaxExpress")
	public void ajaxCountNew(
			@RequestParam(value = "expressNo") String expressNo,
			@RequestParam(value = "warehouseCode") String warehouseCode,
			@RequestParam(value = "expressCode") String expressCode,
			@RequestParam(value = "jobuuid") String jobuuid,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		User user = this.accountService.findUserByJobuuid(jobuuid);//getShiroUser();
		try {
			json = new JSONObject();
			out = response.getWriter();
			String status;
			String content;
			SendOut send = this.sendOutService.findByLogisticNumber(expressNo);
			if(send != null){
				status = "2";
				content = "快递单号重复";
			}else{
				boolean flag = checkLogistic(expressNo,expressCode);
				if(!flag){
					status = "3";
					content = "快递交接失败";
				}else{
					SendOut sendOut = new SendOut();
					sendOut.setLogisticNumber(expressNo);
					sendOut.setLogisticCompany(expressCode);
					sendOut.setWarehouseCode(warehouseCode);
					sendOut.setSendTime(new Date());
					sendOut.setCreationDate(new Date());
					sendOut.setLastUpdateTime(new Date());
					sendOut.setCreatedBy(Integer.valueOf(user.getId().toString()));
					sendOut.setLastUpdatedBy(Integer.valueOf(user.getId().toString()));
					if(sendOut != null){
					    sendOutService.save(sendOut);
						status = "1";
						content = "成功";
					}else{
						status = "3";
						content="快递交接失败";
				    }
				}
			  }
			  json.put("status", status);
			  json.put("content", content);
			} catch (PaiUException e) {
				json.put("status", e.getErrorCode());
				json.put("content", e.getMessage());
			}catch (Exception ex){
				log.error("error",ex);
				json.put("status", "4");
				json.put("content", "服务器异常");
			}
	    log.info("快递单号:" + expressNo);
		out.print(json.toString());
	}
	
	@RequestMapping(value = "ajaxExpress2")
	public void ajaxCountNew2(
			@RequestParam(value = "expressNo") String expressNo,
			@RequestParam(value = "warehouseCode") String warehouseCode,
			@RequestParam(value = "expressCode") String expressCode,
			@RequestParam(value = "jobuuid") String jobuuid,
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		User user = accountService.findUserByJobuuid(jobuuid);// getShiroUser();
		try {
			json = new JSONObject();
			out = response.getWriter();
			String status;
			String content;
			log.info("ajaxCountNew2--快递单号:" + expressNo);
			SendOut send = sendOutService.findByLogisticNumber(expressNo);
			if (null != send) {
				status = "2";
				content = "快递单号重复";
			} else {
				WorkOrder workOrder = this.workOrderService
						.loadByExpressNoAndWarehouseCode(expressNo,
								warehouseCode);
				if (null == workOrder) {
					status = "3";
					content = "快递单和仓库不匹配";
				} else {
					if (!workOrder.getWarehouseCode().equals(warehouseCode)) {
						status = "3";
						content = "仓库不匹配";
					} else {
						SendOut sendOut = new SendOut();
						sendOut.setLogisticNumber(expressNo);
						sendOut.setLogisticCompany(workOrder
								.getExpressCompany());
						sendOut.setCustomerCode(workOrder.getCustomerCode());
						sendOut.setWarehouseCode(workOrder.getWarehouseCode());
						sendOut.setSendTime(new Date());
						sendOut.setCreationDate(new Date());
						sendOut.setLastUpdateTime(new Date());
						sendOut.setCreatedBy(Integer.valueOf(user.getId()
								.toString()));
						sendOut.setLastUpdatedBy(Integer.valueOf(user.getId()
								.toString()));
						sendOutService.save(sendOut);
						status = "1";
						content = "成功";
					}
				}
			}

			json.put("status", status);
			json.put("content", content);
		} catch (Exception e) {
			log.error("error", e);
			json.put("status", "4");
			json.put("content", "服务器异常");
		}
		out.print(json.toString());
	}
	
	/*
	 * 保存快递公司与快递单号对应关系
	 */
	private Map<String,String> getLogisticMap(){
		Map<String,String> logisticMap = new HashMap<String,String>();
		logisticMap.put("997", "TTKDEX");
		logisticMap.put("550", "TTKDEX");
		logisticMap.put("580", "TTKDEX");
		logisticMap.put("330", "STO");
		logisticMap.put("221", "STO");
		logisticMap.put("568", "STO");
		logisticMap.put("401", "ZTO");
		logisticMap.put("531", "ZTO");
		logisticMap.put("765", "ZTO");
		logisticMap.put("VB", "JD");
		logisticMap.put("989", "POSTB");
		logisticMap.put("961", "POSTB");
		logisticMap.put("991", "POSTB");
		logisticMap.put("956", "EMS");
		logisticMap.put("605", "SF");
		logisticMap.put("923", "SF");
		logisticMap.put("922", "SF");
		logisticMap.put("203", "SF");
		logisticMap.put("212", "SF");
		logisticMap.put("211", "HTKY");
		
		//add LiBin 20170919
		logisticMap.put("VB3", "JD");
		logisticMap.put("VD3", "JD");
		logisticMap.put("392", "YUNDA");
		logisticMap.put("391", "YUNDA");
		logisticMap.put("383", "YUNDA");
		return logisticMap;
	}
	
	/**
	 * 
	 * @param logistic
	 * @param expressCode
	 */
	private boolean checkLogistic(String logistic,String expressCode){
		boolean flag;
		if(logistic.length()<3) return false;//add LIBIN 2016/07/11 防止空指针异常
		Map<String,String> logisticMap = getLogisticMap();
		String map = logistic.substring(0,2);
		boolean isNum = map.matches("[0-9]+");
		if(!isNum){
			String express = logisticMap.get(map);
			if(express.equals(expressCode)){
				flag = true;
			}
			else{
				flag = false;
			}
		}else{
		   String map2 = logistic.substring(0,3);
		   String express = logisticMap.get(map2);
		   if(null==express) return false;//add LIBIN 2016/07/11 防止空指针异常
		   if(express.equals(expressCode)){
			   flag = true;
		   }else{
			   flag = false;
		   }
		}
		return flag;
	}
	
	
	
}

  

    
